package com.china08.yxyapi.rabbitmq.listener.swsstatistics;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import com.china08.yxyapi.config.SwsStatisticsConfig;
import com.china08.yxyapi.service.sws.homeschool.impl.YxyMomentsStatisticsServiceImpl;
import com.china08.yxyapi.service.sws.homeschool.impl.YxyNoticeClassStatisticsServiceImpl;
import com.china08.yxyapi.util.DateUtils;

/**
 * 家校互动统计监听
 *
 */
@Component
@EnableRabbit
public class HomeschoolStatisticsListener {

    public static final Logger LOGGER = LoggerFactory.getLogger(HomeschoolStatisticsListener.class);

    @Autowired
    private YxyMomentsStatisticsServiceImpl yxyMomentsStatisticsServiceImpl;

    @Autowired
    private YxyNoticeClassStatisticsServiceImpl yxyNoticeClassStatisticsServiceImpl;

    @Autowired
    private RestTemplate restTemplate;

    @Value(value = "${yx.rest.api.yx.path}")
    private String yxyApiYxUrl;

    /**
     * 业务统计——家校统计
     * 
     * @param date
     * @throws ParseException
     */
    @RabbitListener(queues = SwsStatisticsConfig.YW_SWS_HOMESCHOOL_STATISTICS)
    private void homeschoolStatisticsListener(Integer date) throws ParseException {

        // 班级圈
        yxyMomentsStatisticsServiceImpl.getMomentForClass(date);
        yxyMomentsStatisticsServiceImpl.getMomentForSchool(date);
        yxyMomentsStatisticsServiceImpl.getMomentForUser(date);

        // 班级通知
        yxyNoticeClassStatisticsServiceImpl.getNoticeClassForClass(date);
        yxyNoticeClassStatisticsServiceImpl.getNoticeClassForSchool(date);
        yxyNoticeClassStatisticsServiceImpl.getNoticeClassForUser(date);

        Date dayDate = DateUtils.getBeforeDay(DateUtils.formatStrDate4(date));

        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("dateTime", DateUtils.formatDate4(dayDate));
        HttpEntity<Map<String, Object>> formEntity = new HttpEntity<Map<String, Object>>(map, headers);
        String httpResult = restTemplate.postForObject(yxyApiYxUrl + "/api_yx/comm/school/sws/homeschool/mq",
                formEntity, String.class);
        if (httpResult.contains("success")) {
            System.out.println("班级圈触发mq执行ok,日期：" + DateUtils.formatDate4(dayDate));
        }

    }

}
